package com.ict.txmsv2.utils;

import java.io.*;
import java.net.URL;
import java.util.Date;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-17
 */
public class OSSutils {

    //log日志
    //阿里云API的内或外网域名
    private static String ENDPOINT;
    //阿里云API的密钥Access Key ID
    private static String ACCESS_KEY_ID;
    //阿里云API的密钥Access Key Secret
    private static String ACCESS_KEY_SECRET;
    //阿里云API的bucket名称
    private static String BACKET_NAME;
    //阿里云API的文件夹名称
    private static String FOLDER;
    //初始化属性
    static{
        ENDPOINT = OSSClientConstants.ENDPOINT;
        ACCESS_KEY_ID = OSSClientConstants.ACCESS_KEY_ID;
        ACCESS_KEY_SECRET = OSSClientConstants.ACCESS_KEY_SECRET;
        BACKET_NAME = OSSClientConstants.BACKET_NAME;
        FOLDER = OSSClientConstants.FOLDER;
    }

    /**
     * 获取阿里云OSS客户端对象
     * @return ossClient
     */
    public static OSSClient getOSSClient(){
        return new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    }

    public static String uploadAvatar(MultipartFile file) throws IOException {
        OSSClient ossClient = getOSSClient();
        long l = System.currentTimeMillis();
        String ss = String.valueOf(l)+ ".jpg";
        PutObjectResult putObjectResult = ossClient.putObject(BACKET_NAME, ss, file.getInputStream());
        Date expiration = new Date(System.currentTimeMillis()+3600L * 1000 * 24 * 365 * 10);
        URL url = ossClient.generatePresignedUrl(BACKET_NAME, ss,expiration);
        ossClient.shutdown();
        String[] split = url.toString().split("\\?");
        return split[0];
    }

    public static String uploadEquipPhoto(InputStream inputStream){
        OSSClient ossClient = getOSSClient();
        long l = System.currentTimeMillis();
        String ss = String.valueOf(l)+ ".jpg";
        PutObjectResult putObjectResult = ossClient.putObject(BACKET_NAME, ss,inputStream);
        Date expiration = new Date(System.currentTimeMillis()+3600L * 1000 * 24 * 365 * 10);
        URL url = ossClient.generatePresignedUrl(BACKET_NAME, ss,expiration);
        ossClient.shutdown();
        String[] split = url.toString().split("\\?");
        return split[0];
    }

    public static boolean deleteAvatar(String path){
        String[] split = path.split("/");
        path = split[split.length-1];
        System.out.println(path);
        OSSClient ossClient = getOSSClient();
        ossClient.deleteObject(BACKET_NAME,path);
        ossClient.shutdown();
        return true;
    }

    public static void main(String[] args) throws FileNotFoundException {
        OSSClient ossClient = OSSutils.getOSSClient();
        FileInputStream in = new FileInputStream("C:\\Users\\Yoleen\\Desktop\\微信图片_20201222140324.png");
        PutObjectResult ictLogo = ossClient.putObject(BACKET_NAME, "ictLogo", in);
        URL url = ossClient.generatePresignedUrl(BACKET_NAME, "ictLogo", new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10));
        ossClient.shutdown();
        System.out.println(url.toString());
    }

}
